//
//  MixDatas.swift
//  MTBLETools
//
//  Created by 方强 on 16/11/27.
//  Copyright © 2016年 MT. All rights reserved.
//

import UIKit
import FMDB
import MTSDK

class MixDatas: NSObject {
    public var id:Int = 0;
    public var beaconPwd = "123456";
    public var usertype  = 0;
    
    // 创建表
    public static func createTable(database:FMDatabase) -> Bool{
        guard database.open() else {
            print("Unable to open database");
            return false;
        }
        
        do {
            try database.executeUpdate("create table mixdatas(id integer primary key autoincrement, beaconPwd text, usertype integer)", values: nil);
        } catch {
            MTTools.logout(log: "failed: \(error.localizedDescription)");
            database.close();
            return false;
        }
        
        database.close();
        return true;
    }
    
    // 查询所有绑定设备
    public static func findAll(database:FMDatabase) -> MixDatas?{
        guard database.open() else {
            MTTools.logout(log: "Unable to open database");
            return nil;
        }
        
        let mixdata = MixDatas();
        do {
            let rs = try database.executeQuery("select id, beaconPwd, usertype from mixdatas", values: nil);
            if(rs.next()){
                mixdata.id = Int(rs.int(forColumn: "id"));
                mixdata.beaconPwd = rs.string(forColumn: "beaconPwd");
                mixdata.usertype = Int(rs.int(forColumn: "usertype"));
            }else{
                return nil;
            }
        } catch {
            print("failed: \(error.localizedDescription)");
            database.close();
            return nil;
        }
        
        database.close();
        return mixdata;
    }
    
    // 添加设备
    public static func add(database:FMDatabase, device:MixDatas) -> Int{
        guard database.open() else {
            MTTools.logout(log: "Unable to open database");
            return -1;
        }
        
        var newid = 0;
        
        do {
            try database.executeUpdate("insert into mixdatas (beaconPwd,usertype)  values (?,?) ", values: [device.beaconPwd, NSNumber(value: device.usertype)]);
            newid = Int(database.lastInsertRowId());
        } catch {
            print("failed: \(error.localizedDescription)");
            database.close();
            return -1;
        }
        
        database.close();
        return newid;
    }
    
    // 更新设备
    public static func updata(database:FMDatabase, device:MixDatas) -> Bool{
        guard database.open() else {
            MTTools.logout(log: "Unable to open database");
            return false;
        }
        
        do {
            try database.executeUpdate("update mixdatas set beaconPwd = ?, usertype = ?  WHERE id = ? ", values: [device.beaconPwd, NSNumber(value: device.usertype), NSNumber(value: device.id)]);
        } catch {
            print("failed: \(error.localizedDescription)");
            database.close();
            return false;
        }
        
        database.close();
        return true;
    }
}
